﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 台签打印
{
    static class Extension_Methods
    {
        static readonly float dpiX;
        static readonly float dpiY;

        static Extension_Methods()
        {
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                dpiX = graphics.DpiX;
                dpiY = graphics.DpiY;
            }
        }

        /// <summary>
        /// 显示屏像素转换英寸 1/100英寸
        /// </summary>
        /// <param name="sizeF"></param>
        /// <returns></returns>
        public static SizeF Pixel2Inch(this SizeF sizeF)
        {
            return new SizeF((float)(sizeF.Width * 0.01f * dpiX), (float)(sizeF.Height * 0.01 * dpiY));
            //return sizeF;
        }

        /// <summary>
        ///  //转换单位  毫米转1/100英寸
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Size Millimeter2Inch(this Size size)
        {
            return new Size((int)(size.Width / 2.54 * 10), (int)(size.Height / 2.54 * 10));
            //return size;
        }

        /// <summary>
        ///  //转换单位  毫米转1/100英寸
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Point Millimeter2Inch(this Point point)
        {
            return new Point((int)(point.X / 2.54 * 10), (int)(point.Y / 2.54 * 10));
            //return point;
        }


        /// <summary>
        /// 在指定坐标点绘制十字标记
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="pen"></param>
        /// <param name="point"></param>
        /// <param name="radius"></param>
        public static void DrawCrossMark(this Graphics graphics, Pen pen, Point point, float radius = 5f)
        {
            // 计算线段端点坐标
            float left = point.X - radius;
            float right = point.X + radius;
            float top = point.Y - radius;
            float bottom = point.Y + radius;

            // 绘制水平线
            graphics.DrawLine(pen, left, point.Y, right, point.Y);
            // 绘制垂直线
            graphics.DrawLine(pen, point.X, top, point.X, bottom);
        }


        /// <summary>
        /// 绘制矩型角线
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="pen"></param>
        /// <param name="rectangle"></param>
        public static void DrawRectangleX(this Graphics graphics, Pen pen, Rectangle rectangle, int lineWidth = 5)
        {           
            //左上
            var p1 = rectangle.Location;
            var p2 = p1 + new Size(lineWidth, 0);
            var p3 = p1 + new Size(0, lineWidth);
            graphics.DrawLine(pen, p1, p2);
            graphics.DrawLine(pen, p1, p3);

            //右上
            p1.Offset(rectangle.Width, 0);
            p2 = p1 + new Size(lineWidth * -1, 0);
            p3 = p1 + new Size(0, lineWidth);
            graphics.DrawLine(pen, p1, p2);
            graphics.DrawLine(pen, p1, p3);

            //左下
            p1.Offset(rectangle.Width * -1, rectangle.Height);
            p2 = p1 + new Size(0, lineWidth * -1);
            p3 = p1 + new Size(lineWidth, 0);
            graphics.DrawLine(pen, p1, p2);
            graphics.DrawLine(pen, p1, p3);

            //右下
            p1.Offset(rectangle.Width, 0);
            p2 = p1 + new Size(0, lineWidth * -1);
            p3 = p1 + new Size(lineWidth * -1, 0);
            graphics.DrawLine(pen, p1, p2);
            graphics.DrawLine(pen, p1, p3);
        }

        /// <summary>
        /// 换行处理 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string NewLine(this string str)
        {
            return string.Join(Environment.NewLine, str.SplitTab());
        }

        /// <summary>
        /// 分割成数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static IEnumerable<string> SplitTab(this string s) => s.Split(new[] { '\t' }, StringSplitOptions.None).Select(a => a.Trim());



        /// <summary>
        /// 绘制带百分比边距的分散对齐文本
        /// </summary>
        public static void DrawDistributedString(this Graphics g, string text, Font font, Brush brush, RectangleF rect, float marginRatio)
        {
            if (string.IsNullOrEmpty(text)) return;

            // 参数有效性检查
            marginRatio = Math.Max(0, Math.Min(marginRatio, 0.5f));

            // 计算实际边距值
            float margin = rect.Width * marginRatio;
            float contentWidth = rect.Width - margin;

            // 测量字符宽度
            var charWidths = new List<float>(text.Length);
            foreach (char c in text)
            {
                var charWidth = g.MeasureString(c.ToString(), font, Point.Empty, StringFormat.GenericTypographic).Width;
                charWidths.Add(charWidth);
            }

            // 计算可分配间距
            float totalCharWidth = charWidths.Sum();
            float extraSpacing = contentWidth - totalCharWidth;

            // 处理无法分散的情况
            if (extraSpacing <= 0 || text.Length == 1 || text.Contains(Environment.NewLine))
            {
                // 在内容区域内居中绘制
                var format = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                g.DrawString(text, font, brush, rect, format);
                return;
            }

            // 计算字符间距增量
            float spacingIncrement = extraSpacing / (text.Length - 1 + 2);   //两端留空白间距

            // 绘制分散文本
            float startX = rect.Left + (margin / 2) + spacingIncrement;   //左边加上一个空白间距
            float y = rect.Top + (rect.Height - font.Height) / 2;

            float x = startX;
            for (int i = 0; i < text.Length; i++)
            {
                g.DrawString(text[i].ToString(), font, brush, x, y, StringFormat.GenericTypographic);
                x += charWidths[i] + spacingIncrement;
            }
        }



    }
}
